﻿#define _CRT_SECURE_NO_WARNINGS 1
//写一个程序拷贝文件
//作业内容
//使用所学文件操作，在当前目录下放一个文件data.txt，写一个程序，将data.txt文件拷贝一份，生成data_copy.txt文件。
//基本思路：
//打开文件data.txt，读取数据
//打开文件data_copy.txt，写数据
//从data.txt中读取数据存放到data_copy.txt文件中，直到文件结束。
//关闭两个文件
//#include <stdio.h>
//int main()
//{
//	FILE* pf = fopen("data.txt", "w");//打开data.txt
//	if (pf == NULL)
//	{
//		perror("data.txt-fopen");
//		return 1;
//	}
//	fputs("太NONO了", pf);//写入数据
//	fclose(pf);//关闭data.txt
//	pf = NULL;
//
//	FILE* pf2 = fopen("data_copy.txt", "w");//打开data_copy.txt
//	pf = fopen("data.txt", "r");//打开data.txt
//	if (pf == NULL)
//	{
//		perror("data.txt-fopen");
//		return 1;
//	}
//	if (pf2 == NULL)
//	{
//		perror("data_copy.txt-fopen");
//		return 1;
//	}
//	char tem = 0;//创建临时变量存放从data.txt里读取的数据
//	while ((tem = fgetc(pf)) != EOF)//如果读取完数据返回EOF退出循环
//	{
//		fputc(tem,pf2);//把从data.txt读取的存入data_copy里
//	}
//	fclose(pf);//关闭data.txt
//	fclose(pf2);//关闭data_copy.txt
//	pf = NULL;
//	pf2 = NULL;
//	return 0;
//}
//fgetc 
//fputc 
//fgets 
//fputs 
//fscanf 
//fprintf 
//fread 
//fwrite 
#include <stdio.h>
int main()
{
	
	//fputc 
	FILE* fp = fopen("xxxy.txt", "w");
	if (fp == NULL)
	{
		perror("xxxxy.txt is not");
	}
	fputc('a', fp);
	fputc('b', fp);
	fclose(fp);
	//fgetc 
	fp = fopen("xxxy.txt", "r");
	if (fp == NULL)
	{
		perror("xxxxy.txt is not");
	}
	printf("%c\n", fgetc(fp));
	printf("%c\n", fgetc(fp));
	fclose(fp);

	//fputs 
	fp = fopen("xxxy.txt", "w");
	if (fp == NULL)
	{
		perror("xxxxy.txt is not");
	}
	fputs("xxxy(樱花集)\n", fp);
	fputs("xxxy(CS2)\n", fp);
	fclose(fp);
	
	//fgets 
	char arr[100] = {0};
	fp = fopen("xxxy.txt", "r");
	if (fp == NULL)
	{
		perror("xxxxy.txt is not");
	}
	while (fgets(arr, 100, fp) != NULL)
	{
		printf("%s", arr);
	}
	fclose(fp);
//fscanf 
//fprintf 
	fp = fopen("xxxy.txt", "w+");
	if (fp == NULL)
	{
		perror("xxxxy.txt is not");
	}
	fprintf(fp, "%d %f %s", 47, 25.67, "Ilike");
	int A = 0;
	float B = 0.0;
	rewind(fp);
	fscanf(fp, "%d %f %s", &A, &B, arr);
	printf("%d  %f  %s\n", A, B, arr);
	fclose(fp);

	
//fwrite 
	fp = fopen("xxxy.txt", "wb");
	if (fp == NULL)
	{
		perror("xxxxy.txt is not");
	}
	int arr2[] = { 1,2,3 };
	fwrite(arr2, sizeof(arr2[0]), 3, fp);
	fclose(fp);
	//fread 
	fp = fopen("xxxy.txt", "rb");
	if (fp == NULL)
	{
		perror("xxxxy.txt is not");
	}
	int a = 0;
	
	while (fread(&a, sizeof(int), 1, fp))
		printf("%d\n", a);
	fclose(fp);

//sscanf()
	char str[] = "141 512.25 H";
	int A2 = 0;
	float B2 = 0;
	char C2 = 0;
	sscanf(str, "%d %f %c", &A2, &B2, &C2);
	printf("%d %f %c\n", A2, B2, C2);
//sprintf
	char str2[50] = { 0 };
	sprintf(str2, "%d %f %c", A2, B2, C2);
	printf("%s", str2);
	return 0;
}
